package com.xc.erp.modules.erp.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xc.erp.common.utils.StringUtils;
import com.xc.erp.modules.erp.domain.pojo.JshRole;
import com.xc.erp.modules.erp.domain.pojo.JshRoleFunction;
import com.xc.erp.modules.erp.domain.pojo.JshUserRole;
import com.xc.erp.modules.erp.mapper.JshRoleMapper;
import com.xc.erp.modules.erp.service.JshRoleFunctionService;
import com.xc.erp.modules.erp.service.JshRoleService;
import com.xc.erp.modules.erp.service.JshUserRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
*
*/
@Service
public class JshRoleServiceImpl extends ServiceImpl<JshRoleMapper, JshRole>
implements JshRoleService {

    @Autowired
    private JshRoleFunctionService jshRoleFunctionService;


    @Autowired
    private JshUserRoleService jshUserRoleService;

    /**
     * 分页查询
     * @param pageNum
     * @param pageSize
     * @param name
     * @return
     */
    @Override
    public Page<JshRole> getRoleList(Integer pageNum, Integer pageSize, String name) {
        Page<JshRole> jshRolePage = new Page<>(pageNum, pageSize);
        QueryWrapper<JshRole> queryWrapper = new QueryWrapper<>();
        if (name!=null && !name.equals("")){
            queryWrapper.like("name",name);
        }
        Page<JshRole> page=getBaseMapper().selectPage(jshRolePage,queryWrapper);
        return page;
    }

    /**
     * 批量删除
     * @param ids 数据集合
     */
    @Transactional(rollbackFor = RuntimeException.class)
    @Override
    public void deleteById(Long[] ids) {
        this.baseMapper.deleteById(ids);
    }

    @Override
    public boolean checkIsNameExist(Long id,String name) {
        QueryWrapper<JshRole> queryWrapper=new QueryWrapper();
        if (!StringUtils.isEmpty(name)){
            queryWrapper.eq("name",name);
        }
        JshRole jshRole = baseMapper.selectOne(queryWrapper);

        if (jshRole!=null){
            if (id!=null){
                if (id.equals(jshRole.getId())){
                    return false;
                }
            }
            return true;
        }
        return false;
    }

    @Transactional(rollbackFor = RuntimeException.class)
    @Override
    public void batchSetStatus(Long[] ids, Boolean status) {
        List<JshRole> roleList=new ArrayList<>();
        for (Long id : ids) {
            JshRole role = new JshRole();
            role.setEnabled(status);
            role.setId(id);
            roleList.add(role);
        }
        //批量操作
        this.updateBatchById(roleList);
    }

    @Transactional(rollbackFor = RuntimeException.class)
    @Override
    public boolean deleteByRoleId(Integer id) {
        //删除角色和功能关联
        jshRoleFunctionService.removeByRoleId(id);
        //删除角色信息
        return removeById(id);
    }

    @Override
    public boolean isUserNotNull(Integer[] ids){
        QueryWrapper<JshUserRole> queryWrapper=new QueryWrapper<>();
        queryWrapper.in("jsh_role_id", Arrays.asList(ids));
        Long aLong = jshUserRoleService.getBaseMapper().selectCount(queryWrapper);
        return aLong > 0;
    }

    @Transactional(rollbackFor = RuntimeException.class)
    @Override
    public boolean deleteByRoleIdBatch(Integer[] ids) {
        //删除角色和功能关联表
        QueryWrapper<JshRoleFunction> wrapper = new QueryWrapper<>();
        wrapper.in("jsh_role_id",Arrays.asList(ids));
        jshRoleFunctionService.remove(wrapper);
        //批量删除角色信息
        return removeBatchByIds(Arrays.asList(ids));
    }

}
